from bs4 import BeautifulSoup
import nltk
from collections import Counter, defaultdict


a = BeautifulSoup(open("/home/panagiotis/Downloads/hotels.xml",'r'))

aspects = defaultdict(list)
c = Counter()
total = 0
ngrams = Counter()



for instance in a.findAll("instance"):
	sentence = instance.content.text
	number = int(instance.aspects['number'])
	total += number
	if number == 0:
		c["No Aspects"] += 1
	else:
		for aspect in instance.findAll("aspect"):
			target = aspect['content']
			ngrams[len(nltk.word_tokenize(target))] += 1
			sent = nltk.pos_tag(nltk.word_tokenize(sentence))
			try:
				tpos = sent[[word for (word,pos) in sent].index(target)][1]
			except:
				tpos = "Not Found"
			#for (word, pos) in nltk.pos_tag(nltk.word_tokenize(sentence)):
			#	if word == aspect['content']:
			#		c[pos] += 1
			#		aspects.append(aspect['content'])
			#	else:
			#		c["Not Found"] += 1
			finally:
				c[tpos] += 1
				aspects[tpos].append(target)

print c
print ngrams
for tpos in aspects:
	if not tpos.startswith("NN"):
		print tpos, aspects[tpos]
